<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Main_content {

	private $konten;
	private $type = '';
	private $scripts = array();

	public function __construct(){
		$this->CI =& get_instance();
	}

	public function is_set(){
		if(isset($this->konten)) 
			return true;
		else return false;
	}

	public function set_form_register(){

		$this->type = 'register';
		$this->scripts['register'] = true;

		return $this;

	}

	public function is_form_register(){
		if($this->type == 'register') 
			return true;
		return false;
	}

	public function set_editable(){

		$this->type = 'editable';
		$this->scripts['editable'] = true;

		$this->CI->headbar
			->add_menu('info','Info')
			->add_menu('edit','Edit');
		$this->editable = true;

		return $this;

	}

	public function set_deletable(){

		$this->type = 'editable';
		$this->CI->headbar->add_menu('delete','Delete');

		return $this;
	}

	public function is_editable(){
		if($this->type == 'editable') 
			return true;
		return false;
	}

	function add_script($script_name){
		$this->scripts[$script_name] = true;
		return $this;
	}

	public function get_scripts(){
		return $this->scripts;
	}

	public function add_custom($tag,$isi,$id = '' ,$tambahan = ''){

		if($id != '')
			$id = "id = '$id'";

		$data = "<$tag $id $tambahan> $isi </$tag>";

		$this->konten[] = $data;
		return $this;
	}

	public function add_block($block = '',$id = '', $class = ''){

		if(is_string($block)){
			$block = new Block($block, $id, $class);
		} 

		if(is_object($block)){
			if(get_class($block) == 'Block'){
				$this->konten[] = $block;
			}
		} else return false;

		return $block;
	}

	public function add_grid($grid = 5, $alpha = false, $omega = false){

		if(is_int($grid)){
			$grid = new Grid($grid,$alpha,$omega);
		} 

		if(is_object($grid)){
			if(get_class($grid) == 'Grid'){
				$this->konten[] = $grid;
			}
		} else return false;

		return $grid;
	}

	public function add_pagination(){
		$this->konten[] = $this->CI->pagination->create_links();
		return $this;
	}

	public function add_calendar($calendar){

		$this->scripts['calendar'] = true;
		$this->konten[] = "<div class='calendar'>$calendar</div>";

		return $this;
	}

	private function error_block(){

		$header = heading('Error!',4);

		$error_block = 
			"<div class='block' id='error_info' style='display: none'>
				$header 
				<div></div> 
			</div>";

		return $error_block;
	}


	public function __toString(){

		if($this->CI->sidebar->is_set())
			$string = "<div id='mainContent' class='half'>";
		else $string = "<div id='mainContent' class='full'>";

		if(isset($this->scripts['editable']))
			$string .= form_open('','id = edit_form');

		if(isset($this->scripts['register']))
			$string .= form_open('','id = register_form');

		foreach($this->konten as $isi){
			$string .= $isi;
		}

		$string .= $this->error_block();

		if(isset($this->scripts['editable'])){
			$string .= form_submit('','Ubah','style = "display:none"');
			$string .= form_close();
		}

		if(isset($this->scripts['register'])) {
			$string .= form_submit('','Register');
			$string .= form_close(); 
		}

		$string .= "</div>";

		return $string;
	}

}
